home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / guard.py < prev    next >
Text File  |  2008-02-01  |  21KB  |  578 lines

  1. # -*- test-case-name: nevow.test.test_guard -*-
  2. # Copyright (c) 2004-2008 Divmod.
  3. # See LICENSE for details.
  4.  
  5. """
  6. Resource protection for Nevow. If you wish to use twisted.cred to protect your
  7. Nevow application, you are probably most interested in
  8. L{SessionWrapper}.
  9. """
  10.  
  11. __metaclass__ = type
  12.  
  13. import random
  14. import time
  15. import md5
  16. import StringIO
  17.  
  18. from zope.interface import implements
  19.  
  20. # Twisted Imports
  21.  
  22. from twisted.python import log, components
  23. from twisted.internet import defer
  24. from twisted.cred.error import UnauthorizedLogin
  25. from twisted.cred.credentials import UsernamePassword, Anonymous
  26.  
  27. try:
  28.     from twisted.web import http
  29. except ImportError:
  30.     from twisted.protocols import http
  31.  
  32. # Nevow imports
  33. from nevow import inevow, url, stan
  34.  
  35.  
  36. def _sessionCookie():
  37.     return md5.new("%s_%s" % (str(random.random()) , str(time.time()))).hexdigest()
  38.  
  39.  
  40. class GuardSession(components.Componentized):
  41.     """A user's session with a system.
  42.  
  43.     This utility class contains no functionality, but is used to
  44.     represent a session.
  45.     """
  46.     implements(inevow.ISession, inevow.IGuardSession)
  47.  
  48.     def __init__(self, guard, uid):
  49.         """Initialize a session with a unique ID for that session.
  50.         """
  51.         components.Componentized.__init__(self)
  52.         self.guard = guard
  53.         self.uid = uid
  54.         self.expireCallbacks = []
  55.         self.checkExpiredID = None
  56.         self.setLifetime(60)
  57.         self.portals = {}
  58.         self.touch()
  59.  
  60.     # New Guard Interfaces
  61.  
  62.     def getLoggedInRoot(self):
  63.         """Get the most-recently-logged-in avatar.
  64.         """
  65.         # XXX TODO: need to actually sort avatars by login order!
  66.         if len(self.portals) != 1:
  67.             raise RuntimeError("Ambiguous request for current avatar.")
  68.         return self.portals.values()[0][0]
  69.  
  70.     def resourceForPortal(self, port):
  71.         return self.portals.get(port)
  72.  
  73.     def setDefaultResource(self, rsrc, logout):
  74.         """
  75.  
  76.         Change the root-resource available to the user who has already
  77.         authenticated anonymously.  This only works in applications that DO NOT
  78.         use the multiple-simultaneous-portals feature.  If you do not know what
  79.         this means, you may safely ignore it.
  80.  
  81.         """
  82.         if len(self.portals) != 1:
  83.             raise RuntimeError("Ambiguous request for current avatar.")
  84.         self.setResourceForPortal(
  85.             rsrc,
  86.             self.portals.keys()[0],
  87.             logout)
  88.  
  89.     def setResourceForPortal(self, rsrc, port, logout):
  90.         """Change the root-resource available to a user authenticating against a given
  91.         portal.
  92.  
  93.         If a user was already logged in to this session from that portal, first
  94.         log them out.
  95.  
  96.         @param rsrc: an L{IResource} implementor.
  97.         @param port: a cred Portal instance.
  98.         @param logout: a 0-arg callable to be invoked upon logout.
  99.         """
  100.         self.portalLogout(port)
  101.         self.portals[port] = rsrc, logout
  102.         return rsrc
  103.  
  104.     def portalLogout(self, port):
  105.         """
  106.         If we have previously acccepted a login for this portal, call its
  107.         logout method and de-associate that portal from this session, catching
  108.         any errors from the logout method.
  109.  
  110.         Otherwise: do nothing.
  111.  
  112.         @param port: a cred Portal.
  113.         """
  114.         p = self.portals.get(port)
  115.         if p:
  116.             log.msg('Logout of portal %r' % port)
  117.             r, l = p
  118.             try:
  119.                 l()
  120.             except:
  121.                 log.err()
  122.             del self.portals[port]
  123.  
  124.     # timeouts and expiration
  125.  
  126.     def setLifetime(self, lifetime):
  127.         """Set the approximate lifetime of this session, in seconds.
  128.  
  129.         This is highly imprecise, but it allows you to set some general
  130.         parameters about when this session will expire.  A callback will be
  131.         scheduled each 'lifetime' seconds, and if I have not been 'touch()'ed
  132.         in half a lifetime, I will be immediately expired.
  133.         """
  134.         self.lifetime = lifetime
  135.  
  136.     def notifyOnExpire(self, callback):
  137.         """Call this callback when the session expires or logs out.
  138.         """
  139.         self.expireCallbacks.append(callback)
  140.  
  141.     def expire(self):
  142.         """Expire/logout of the session.
  143.         """
  144.         log.msg("expired session %s" % str(self.uid))
  145.         del self.guard.sessions[self.uid]
  146.  
  147.         # Logout of all portals
  148.         for portal in self.portals.keys():
  149.             self.portalLogout(portal)
  150.  
  151.         for c in self.expireCallbacks:
  152.             try:
  153.                 c()
  154.             except:
  155.                 log.err()
  156.         self.expireCallbacks = []
  157.         if self.checkExpiredID:
  158.             self.checkExpiredID.cancel()
  159.             self.checkExpiredID = None
  160.  
  161.     def touch(self):
  162.         self.lastModified = time.time()
  163.  
  164.     def checkExpired(self):
  165.         # Import reactor here to avoid installing default at startup
  166.         from twisted.internet import reactor
  167.         self.checkExpiredID = None
  168.         # If I haven't been touched in 15 minutes:
  169.         if time.time() - self.lastModified > self.lifetime / 2:
  170.             if self.guard.sessions.has_key(self.uid):
  171.                 self.expire()
  172.             else:
  173.                 log.msg("no session to expire: %s" % str(self.uid))
  174.         else:
  175.             log.msg("session given the will to live for %s more seconds" % self.lifetime)
  176.             self.checkExpiredID = reactor.callLater(self.lifetime,
  177.                                                     self.checkExpired)
  178.     def __getstate__(self):
  179.         d = self.__dict__.copy()
  180.         if d.has_key('checkExpiredID'):
  181.             del d['checkExpiredID']
  182.         return d
  183.  
  184.     def __setstate__(self, d):
  185.         self.__dict__.update(d)
  186.         self.touch()
  187.         self.checkExpired()
  188.  
  189.  
  190. def urlToChild(ctx, *ar, **kw):
  191.     u = url.URL.fromContext(ctx)
  192.     for segment in ar:
  193.         u = u.child(stan.xml(segment))
  194.     if inevow.IRequest(ctx).method == 'POST':
  195.         u = u.clear()
  196.     for k,v in kw.items():
  197.         u = u.replace(k, v)
  198.  
  199.     return u
  200.  
  201.  
  202. SESSION_KEY = '__session_key__'
  203. LOGIN_AVATAR = '__login__'
  204. LOGOUT_AVATAR = '__logout__'
  205.  
  206.  
  207. def nomind(*args): return None
  208.  
  209. class Forbidden(object):
  210.     implements(inevow.IResource)
  211.  
  212.     def locateChild(self, ctx, segments):
  213.         return self
  214.  
  215.     def renderHTTP(self, ctx):
  216.         request = inevow.IRequest(ctx)
  217.         request.setResponseCode(http.FORBIDDEN)
  218.         return ("<html><head><title>Forbidden</title></head>"
  219.                 "<body><h1>Forbidden</h1>Request was forbidden.</body></html>")
  220.  
  221. class SessionWrapper:
  222.     """
  223.     SessionWrapper
  224.  
  225.     The following class attributes can be modified on an instance
  226.     of the class.
  227.  
  228.     @ivar secureCookies: Whether to use secure (TLS only) cookies or not.
  229.       True (default): make cookies secure when session is initiated
  230.       in a secure (TLS) connection.
  231.  
  232.       False: cookies do not get the secure attribute.
  233.  
  234.     @ivar: persistentCookies: Whether to use persistent (saved to disk) cookies or not.
  235.       True: make cookies persistent, so they are valid for the
  236.         length of the sessionLifetime even if the browser window
  237.         is closed.
  238.  
  239.       False (default): cookies do not get saved to disk, and thus last
  240.         only as long as the session does.  If the browser is
  241.         closed before the session timeout, both the session
  242.         and the cookie go away.
  243.     """
  244.     implements(inevow.IResource)
  245.  
  246.     sessionLifetime = 3600
  247.     sessionFactory = GuardSession
  248.  
  249.     # The interface to cred for when logging into the portal
  250.     credInterface = inevow.IResource
  251.  
  252.     useCookies = True
  253.     secureCookies = True
  254.     persistentCookies = False
  255.  
  256.     def __init__(self, portal, cookieKey=None, mindFactory=None, credInterface=None, useCookies=None):
  257.         self.portal = portal
  258.         if cookieKey is None:
  259.             cookieKey = "woven_session_" + _sessionCookie()
  260.         self.cookieKey = cookieKey
  261.         self.sessions = {}
  262.         if mindFactory is None:
  263.             mindFactory = nomind
  264.         self.mindFactory = mindFactory
  265.         if credInterface is not None:
  266.             self.credInterface = credInterface
  267.         if useCookies is not None:
  268.             self.useCookies = useCookies
  269.         # Backwards compatibility; remove asap
  270.         self.resource = self
  271.  
  272.     def renderHTTP(self, ctx):
  273.         request = inevow.IRequest(ctx)
  274.         d = defer.maybeDeferred(self._delegate, ctx, [])
  275.         def _cb((resource, segments), ctx):
  276.             assert not segments
  277.             res = inevow.IResource(resource)
  278.             return res.renderHTTP(ctx)
  279.         d.addCallback(_cb, ctx)
  280.         return d
  281.  
  282.     def locateChild(self, ctx, segments):
  283.         request = inevow.IRequest(ctx)
  284.         path = segments[0]
  285.         if self.useCookies:
  286.             cookie = request.getCookie(self.cookieKey)
  287.         else:
  288.             cookie = ''
  289.  
  290.         if path.startswith(SESSION_KEY):
  291.             key = path[len(SESSION_KEY):]
  292.             if key not in self.sessions:
  293.                 return urlToChild(ctx, *segments[1:], **{'__start_session__':1}), ()
  294.             self.sessions[key].setLifetime(self.sessionLifetime)
  295.             if cookie == key:
  296.                 # /sessionized-url/${SESSION_KEY}aef9c34aecc3d9148/foo
  297.                 #                  ^
  298.                 #                  we are this getChild
  299.                 # with a matching cookie
  300.                 self.sessions[key].sessionJustStarted = True
  301.                 return urlToChild(ctx, *segments[1:]), ()
  302.             else:
  303.                 # We attempted to negotiate the session but failed (the user
  304.                 # probably has cookies disabled): now we're going to return the
  305.                 # resource we contain.  In general the getChild shouldn't stop
  306.                 # there.
  307.                 # /sessionized-url/${SESSION_KEY}aef9c34aecc3d9148/foo
  308.                 #                  ^ we are this getChild
  309.                 # without a cookie (or with a mismatched cookie)
  310.                 return self.checkLogin(ctx, self.sessions[key],
  311.                                        segments[1:],
  312.                                        sessionURL=segments[0])
  313.         else:
  314.             # /sessionized-url/foo
  315.             #                 ^ we are this getChild
  316.             # with or without a session
  317.             return self._delegate(ctx, segments)
  318.  
  319.     def _delegate(self, ctx, segments):
  320.         """Identify the session by looking at cookies and HTTP auth headers, use that
  321.         session key to identify the wrapped resource, then return a deferred
  322.         which fires a 2-tuple of (resource, segments) to the top-level
  323.         redirection code code which will delegate IResource's renderHTTP or
  324.         locateChild methods to it
  325.         """
  326.         request = inevow.IRequest(ctx)
  327.         cookie = request.getCookie(self.cookieKey)
  328.         # support HTTP auth, no redirections
  329.         userpass = request.getUser(), request.getPassword()
  330.         httpAuthSessionKey = 'HTTP AUTH: %s:%s' % userpass
  331.  
  332.         for sessionKey in cookie, httpAuthSessionKey:
  333.             if sessionKey in self.sessions:
  334.                 session = self.sessions[sessionKey]
  335.                 return self.checkLogin(ctx, session, segments)
  336.         # without a session
  337.  
  338.         if userpass != ('',''):
  339.             # the user is trying to log in with HTTP auth, but they don't have
  340.             # a session.  So, make them one.
  341.             sz = self.sessions[httpAuthSessionKey] = self.sessionFactory(self, httpAuthSessionKey)
  342.             # kick off the expiry timer.
  343.             sz.checkExpired()
  344.             return self.checkLogin(ctx, sz, segments, None, UsernamePassword(*userpass))
  345.  
  346.         # no, really, without a session
  347.         ## Redirect to the URL with the session key in it, plus the segments of the url
  348.         rd = self.createSession(ctx, segments)
  349.         return rd, ()
  350.  
  351.     def createSession(self, ctx, segments):
  352.         """
  353.         Create a new session for this request, and redirect back to the path
  354.         given by segments.
  355.         """
  356.  
  357.         request = inevow.IRequest(ctx)
  358.  
  359.         newCookie = _sessionCookie()
  360.         if self.useCookies:
  361.             if self.secureCookies and request.isSecure():
  362.                 secure = True
  363.             else:
  364.                 secure = False
  365.             if self.persistentCookies and self.sessionLifetime:
  366.                 expires = http.datetimeToString(time.time() + self.sessionLifetime)
  367.             else:
  368.                 expires = None
  369.             request.addCookie(self.cookieKey, newCookie,
  370.                               path="/%s" % '/'.join(request.prepath),
  371.                               secure=secure, expires=expires,
  372.                               domain=self.cookieDomainForRequest(request))
  373.         sz = self.sessions[newCookie] = self.sessionFactory(self, newCookie)
  374.         sz.args = request.args
  375.         sz.fields = request.fields
  376.         sz.method = request.method
  377.         sz.received_headers = request.received_headers
  378.         sz.checkExpired()
  379.         return urlToChild(ctx, SESSION_KEY+newCookie, *segments)
  380.  
  381.     def checkLogin(self, ctx, session, segments, sessionURL=None, httpAuthCredentials=None):
  382.         """
  383.         Associate the given request with the given session and:
  384.  
  385.             - log the user in to our portal, if they are accessing a login URL
  386.  
  387.             - log the user out from our portal (calling their logout callback),
  388.               if they are logged in and accessing a logout URL
  389.  
  390.             - Move the request parameters saved on the session, if there are
  391.               any, onto the request if a session just started or a login
  392.               just succeeded.
  393.  
  394.         @return:
  395.  
  396.             - if the user is already logged in: a 2-tuple of requestObject,
  397.               C{segments} (i.e. the segments parameter)
  398.  
  399.             - if the user is not logged in and not logging in, call login() to
  400.               initialize an anonymous session, and return a 2-tuple of
  401.               (rootResource, segments-parameter) from that anonymous session.
  402.               This counts as logging in for the purpose of future calls to
  403.               checkLogin.
  404.  
  405.             - if the user is accessing a login URL: a 2-tuple of the logged in
  406.               resource object root and the remainder of the segments (i.e. the
  407.               URL minus __login__) to be passed to that resource.
  408.  
  409.         """
  410.         request = inevow.IRequest(ctx)
  411.         session.touch()
  412.         request.session = session
  413.         root = url.URL.fromContext(request)
  414.         if sessionURL is not None:
  415.             root = root.child(sessionURL)
  416.         request.rememberRootURL(str(root))
  417.  
  418.         spoof = False
  419.         if getattr(session, 'sessionJustStarted', False):
  420.             del session.sessionJustStarted
  421.             spoof = True
  422.         if getattr(session, 'justLoggedIn', False):
  423.             del session.justLoggedIn
  424.             spoof = True
  425.         if spoof and hasattr(session, 'args'):
  426.             request.args = session.args
  427.             request.fields = session.fields
  428.             request.content = StringIO.StringIO()
  429.             request.content.close()
  430.             request.method = session.method
  431.             request.received_headers = session.received_headers
  432.  
  433.             del session.args, session.fields, session.method, session.received_headers
  434.  
  435.  
  436.         if segments and segments[0] in (LOGIN_AVATAR, LOGOUT_AVATAR):
  437.             authCommand = segments[0]
  438.         else:
  439.             authCommand = None
  440.  
  441.         if httpAuthCredentials:
  442.             # This is the FIRST TIME we have hit an HTTP auth session with our
  443.             # credentials.  We are going to perform login.
  444.             assert not authCommand, (
  445.                 "HTTP auth support isn't that robust.  "
  446.                 "Come up with something to do that makes sense here.")
  447.             return self.login(request, session, httpAuthCredentials, segments).addErrback(
  448.                 self.authRequiredError, session
  449.                 )
  450.  
  451.         if authCommand == LOGIN_AVATAR:
  452.             subSegments = segments[1:]
  453.             def unmangleURL((res,segs)):
  454.                 # Tell the session that we just logged in so that it will
  455.                 # remember form values for us.
  456.                 session.justLoggedIn = True
  457.                 # Then, generate a redirect back to where we're supposed to be
  458.                 # by looking at the root of the site and calculating the path
  459.                 # down from there using the segments we were passed.
  460.                 u = url.URL.fromString(request.getRootURL())
  461.                 for seg in subSegments:
  462.                     u = u.child(seg)
  463.                 return u, ()
  464.             return self.login(request, session, self.getCredentials(request), subSegments).addCallback(
  465.                 unmangleURL).addErrback(
  466.                 self.incorrectLoginError, ctx, subSegments, "Incorrect login."
  467.                 )
  468.         elif authCommand == LOGOUT_AVATAR:
  469.             self.explicitLogout(session)
  470.             return urlToChild(ctx, *segments[1:]), ()
  471.         else:
  472.             r = session.resourceForPortal(self.portal)
  473.             if r:
  474.                 ## Delegate our getChild to the resource our portal says is the right one.
  475.                 return r[0], segments
  476.             else:
  477.                 # XXX I don't think that the errback here will work at all,
  478.                 # because the redirect loop would be infinite.  Perhaps this
  479.                 # should be closer to the HTTP auth path?
  480.                 return self.login(request, session, Anonymous(), segments).addErrback(
  481.                     self.incorrectLoginError, ctx, segments, 'Anonymous access not allowed.')
  482.  
  483.     def explicitLogout(self, session):
  484.         """Hook to be overridden if you care about user-requested logout.
  485.  
  486.         Note: there is no return value from this method; it is purely a way to
  487.         provide customized behavior that distinguishes between session-expiry
  488.         logout, which is what 99% of code cares about, and explicit user
  489.         logout, which you may need to be notified of if (for example) your
  490.         application sets other HTTP cookies which refer to server-side state,
  491.         and you want to expire that state in a manual logout but not with an
  492.         automated logout.  (c.f. Quotient's persistent sessions.)
  493.  
  494.         If you want the user to see a customized logout page, just generate a
  495.         logout link that looks like
  496.  
  497.             http://your-site.example.com/__logout__/my/custom/logout/stuff
  498.  
  499.         and the user will see
  500.  
  501.             http://your-site.example.com/my/custom/logout/stuff
  502.  
  503.         as their first URL after becoming anonymous again.
  504.         """
  505.         session.portalLogout(self.portal)
  506.  
  507.     def getCredentials(self, request):
  508.         username = request.args.get('username', [''])[0]
  509.         password = request.args.get('password', [''])[0]
  510.         return UsernamePassword(username, password)
  511.  
  512.     def login(self, request, session, credentials, segments):
  513.         """
  514.  
  515.         - Calls login() on our portal.
  516.  
  517.         - creates a mind from my mindFactory, with the request and credentials
  518.  
  519.         - Associates the mind with the given session.
  520.  
  521.         - Associates the resource returned from my portal's login() with my
  522.           portal in the given session.
  523.  
  524.         @return: a Deferred which fires a 2-tuple of the resource returned from
  525.         my portal's login() and the passed list of segments upon successful
  526.         login.
  527.  
  528.         """
  529.         mind = self.mindFactory(request, credentials)
  530.         session.mind = mind
  531.         return self.portal.login(credentials, mind, self.credInterface).addCallback(
  532.             self._cbLoginSuccess, session, segments
  533.         )
  534.  
  535.     def _cbLoginSuccess(self, (iface, res, logout), session, segments):
  536.         session.setResourceForPortal(res, self.portal, logout)
  537.         return res, segments
  538.  
  539.     def incorrectLoginError(self, error, ctx, segments, loginFailure):
  540.         """ Used as an errback upon failed login, returns a 2-tuple of a failure URL
  541.         with the query argument 'login-failure' set to the parameter
  542.         loginFailure, and an empty list of segments, to redirect to that URL.
  543.         The basis for this error URL, i.e. the part before the query string, is
  544.         taken either from the 'referer' header from the given request if one
  545.         exists, or a computed URL that points at the same page that the user is
  546.         currently looking at to attempt login.  Any existing query string will
  547.         be stripped.
  548.         """
  549.         request = inevow.IRequest(ctx)
  550.         error.trap(UnauthorizedLogin)
  551.         referer = request.getHeader("referer")
  552.         if referer is not None:
  553.             u = url.URL.fromString(referer)
  554.         else:
  555.             u = urlToChild(ctx, *segments)
  556.  
  557.         u = u.clear()
  558.         u = u.add('login-failure', loginFailure)
  559.         return u, ()
  560.  
  561.     def authRequiredError(self, error, session):
  562.         session.expire()
  563.         error.trap(UnauthorizedLogin)
  564.         return Forbidden(), ()
  565.  
  566.  
  567.     def cookieDomainForRequest(self, request):
  568.         """
  569.         Specify the domain restriction on the session cookie.
  570.  
  571.         @param request: The request object in response to which a cookie is
  572.             being set.
  573.  
  574.         @return: C{None} or a C{str} giving the domain restriction to set on
  575.             the cookie.
  576.         """
  577.         return None
  578.